package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.SalesOrder;
import com.example.service.SalesOrderService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("salesOrders")
public class SalesOrderController {


    @Autowired
    private SalesOrderService salesOrderService;


    /**
     * 分页查询所有销售单信息
     * @return
     */

    @GetMapping("/getPageSalesOrder/{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,SalesOrder salesOrder){
        IPage page=new Page(currentPage,pageSize);
        LambdaQueryWrapper<SalesOrder> lqw =new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(salesOrder.getJobNumber()),SalesOrder::getJobNumber,salesOrder.getJobNumber());
        lqw.like(Strings.isNotEmpty(salesOrder.getName()),SalesOrder::getName,salesOrder.getName());

        IPage page1 = salesOrderService.page(page,lqw);


        return R.success("分页查询所有salesOrders成功！",page1);
    }

    /**
     * 分页查询所有销售单信息
     * @return
     */

    @GetMapping("/getAllSalesOrder")
    public R getAllSalesOrder(){

        List<SalesOrder> list = salesOrderService.list();

        return R.success("分页查询所有salesOrders成功！",list);
    }



//    /**
//     * 查询当前销售单
//     * @return
//     */
//
//    @GetMapping("/getMyself/{id}")
//
//    public R getOne(@PathVariable int id){
//
//        SalesOrder salesOrder= salesOrderService.getOne(id);
//
//        return R.success("分页查询所有salesOrder成功！",salesOrder);
//    }
//
//
    /**
     * 添加
     * @param salesOrder
     * @return
     */
    @PostMapping("/addSalesOrder")
    public R add(@RequestBody SalesOrder salesOrder){

        Date date = new Date();   //获取当前的日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String dateNow = simpleDateFormat.format(date);//获取String类型的时间
        salesOrder.setTime(dateNow);


        System.out.println("==========");
        System.out.println(salesOrder);
        System.out.println("==========");

        Boolean update = salesOrderService.save(salesOrder);
        if (update) {
            return R.success("添加salesOrder成功!", null);
        }
        return R.error("保存销售单失败");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteSalesOrder/{id}")
    public R deleteById(@PathVariable int id){

        Boolean byId = salesOrderService.removeById(id);
        if (byId){
            return R.success("删除salesOrders成功!",null);
        }
        return R.error("删除salesOrders失败!");
    }

    /**
     * 修改
     * @param salesOrder
     * @return
     */
    @PutMapping("/updateSalesOrder")
    public R deleteById(@RequestBody SalesOrder salesOrder){

        Boolean update = salesOrderService.updateById(salesOrder);
        if (update){
            return R.success("修改salesOrder成功!",salesOrder);
        }
        return R.error("修改salesOrder失败!");
    }

    /**
     * 商品销售数量
     * @return
     */

    @GetMapping("/getGoodsNumber")
    public R getGoodsNumber(){

        List<SalesOrder> list = salesOrderService.getGoodsNumber();

        return R.success("商品销售数量成功！",list);
    }
    /**
     * 销售员销售总金额
     * @return
     */

    @GetMapping("/getTotalForSalesman")
    public R getTotalForSalesman(){

        List<SalesOrder> list = salesOrderService.getTotalForSalesman();

        return R.success("销售员销售总金额成功！",list);
    }


}

